package com.jk1.bianligou.blg.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk1.bianligou.blg.aop.MerchantCheck;
import com.jk1.bianligou.blg.common.BaseResponse;
import com.jk1.bianligou.blg.common.ErrorCode;
import com.jk1.bianligou.blg.domain.dto.user.UserLoginRequest;
import com.jk1.bianligou.blg.domain.dto.user.UserRegisterDTO;
import com.jk1.bianligou.blg.domain.dto.user.UserUpdateRequest;
import com.jk1.bianligou.blg.domain.po.User;
import com.jk1.bianligou.blg.exception.BizException;
import com.jk1.bianligou.blg.service.UserService;
import com.jk1.bianligou.blg.utils.ResultUtils;
import com.jk1.bianligou.blg.utils.ThrowUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<User> login(@RequestBody UserLoginRequest request, HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIfParamsIsNull(request);
        String username = request.getUsername();
        String password = request.getPassword();
        User user = userService.login(username, password, httpServletRequest);
        if (user != null) {
            user.setPassword(null);
            return ResultUtils.success(user);
        } else {
            return ResultUtils.error(40001, "用户名或密码错误");
        }
    }

    /**
     * 用户注册
     *
     * @param userRegisterDTO 用户注册传递的请求体
     * @return 返回注册用户id
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("user register {}", userRegisterDTO);
        String userAccount = userRegisterDTO.getUsername();
        String userPassword = userRegisterDTO.getPassword();
        String ensurePassword = userRegisterDTO.getEnsurePassword();
        if (StrUtil.isAllBlank(userAccount, userPassword, ensurePassword)) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "账号或密码为空");
        }
        Long data = userService.userRegister(userAccount, userPassword, ensurePassword);
        return ResultUtils.success(data);
    }


    @ApiOperation("获取分页用户列表")
    @GetMapping("/")
    @MerchantCheck
    public BaseResponse<List<User>> getAllUsers(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        List<User> users = userService.list(new Page<>(page, size));
        return ResultUtils.success(users);
    }


    @ApiOperation("通过id获取用户")
    @GetMapping("/{id}")
    @MerchantCheck
    public BaseResponse<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return ResultUtils.success(user);
    }


    // 更新用户信息
    @ApiOperation("更新用户信息")
    @PutMapping()
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest request) {
        userService.updateUser(request);
        return ResultUtils.success(true);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    @MerchantCheck
    public BaseResponse<Boolean> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ResultUtils.success(true);
    }

    @PostMapping("/{id}")
    public BaseResponse<Boolean> sign(@PathVariable Long id) {
        ThrowUtils.throwIfParamsIsNull(id);
        boolean success = userService.sign(id);
        return ResultUtils.success(success);
    }


}
